<?php


namespace app\api\controller\v1;


use app\api\model\BikeModel;
use app\api\model\BikeRepairModel;
use app\api\model\WarehouseModel;
use app\common\controller\UserBaseController;
use app\common\library\DLock;

class BrorderController extends UserBaseController {

    function addAction() {
        $this->paramlist = array(
            'token' => 'string',
            'bike_id' => 'string',
            'fault_types' => 'intarray',
            'fault_descs' => 'stringarray',
        );

        $this->check_params();
        $this->check_token();

        $lock = new DLock($this->argv['bike_id'], 60);

        $bike_info = BikeModel::instance()->get_info_by_id($this->argv['bike_id']);
        if (empty($bike_info)) {
            $this->throw_myexception(E_ERROR, "get bike info error!");
        }

        $info = (new BikeRepairModel())->add($this->city_id, $this->argv['bike_id'], $this->argv['fault_types'], $this->argv['fault_descs']);
        $this->resp['info'] = $info;
    }

    function recallAction() {
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
            'warehouse_id' => 'string',
            'user_coordinate' => 'floatarray',
        );

        $this->check_params();
        $this->check_token();

        $lock = new DLock($this->argv['order_id'], 60);
        $warehouse_info = (new WarehouseModel())->query_info($this->city_id, $this->argv['warehouse_id']);
        if (empty($warehouse_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询仓库失败!");
        }

        $order_info = (new BikeRepairModel())->query_info($this->city_id, $this->argv['order_id']);
        if (empty($order_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询订单失败!");
        }
        (new BikeRepairModel())->recall($order_info, $warehouse_info['id'], $warehouse_info['name']);
    }

    function repairAction() {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'order_id' => 'string',
            'fault_types' => 'intarray',
            'fault_subtypes' => 'intarray',
            'fault_descs' => 'stringarray',
            'user_coordinate' => 'floatarray',
        );

        $this->check_params();
        $this->check_token();

        $lock = new DLock($this->argv['order_id'], 60);
        $order_info = (new BikeRepairModel())->query_info($this->city_id, $this->argv['order_id']);
        if (empty($order_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "get order info error");
        }
        (new BikeRepairModel())->repaire($order_info, $this->argv['fault_types'], $this->argv['fault_subtypes'], $this->argv['fault_descs']);
    }

    function listAction() {
        $this->paramlist = array(
            'token' => 'string',
            'is_order_finished' => 'optint',
            'time_interval_type' => 'optint',
            'start_time' => 'optstring',
            'end_time' => 'optstring',
            'is_recalled' => 'optint',
            'report_fault_type' => 'optint',
            'actual_fault_type' => 'optint',
            'actual_fault_subtype' => 'optint',
            'bike_model' => 'optstring',
            'warehouse_id' => 'optstring',
            'duration_interval' => 'optstring',
            'id' => 'optstring',
            'bike_sn' => 'optstring',
            'bike_id' => 'optstring',
            'order_state' => 'optint',
            'user_id' => 'optstring',
            'pno' => 'int',
            'psize' => 'int',
        );

        $this->check_params();
        $this->check_token();

        $list = (new BikeRepairModel())->query_list($this->city_id, $this->argv, $this->argv['pos'], $this->argv['limit']);
        $this->resp['list'] = $list;
        $this->resp['total'] = count($list);
    }

}
